package com.sahadev.mvp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.sahadev.mvp.other.BaseRecyclerAdapter;
import com.sahadev.mvp.presenter.Presenter;
import com.sahadev.mvp.view.IView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class MVPActivity extends AppCompatActivity implements IView {
    public final static String KEY = "Presenter";

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private Presenter mPresenter;
    private RecyclerView.Adapter mAdapter;


    public interface IRecyclerView{
        /**获取RecyclerView的LayoutManager，依赖外部注入
         * @param context
         * @return
         */
        public RecyclerView.LayoutManager getLayoutManager(Context context);

        /**获取RecyclerView的Adapter，也是依赖外部注入
         * @param context
         * @return
         */
        public BaseRecyclerAdapter getAdapter(Context context);
    }

    /**
     * 我们的入口
     *
     * @param context
     * @param c       Presenter具体的实现类
     */
    public static void startInstance(Context context, Class c) {
        Intent intent = new Intent(context, MVPActivity.class);
        intent.putExtra(KEY, c);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Class<?> c = (Class<?>) getIntent().getSerializableExtra(KEY);

        try {
            Constructor<?> constructor = c.getConstructor(IView.class);
            Object o = constructor.newInstance(this);
            if (o instanceof Presenter)
                mPresenter = (Presenter) o;
        } catch (NoSuchMethodException e) {


        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (mPresenter == null)
            //如果mPresenter没有实例化成功，那就什么都不用做了
            return;

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //当下拉刷新的时候，调用mPresenter进行数据刷新，使得mPresenter调用其它实现方式来刷新
                mPresenter.requestData(0);//由于是下拉，所以这里的索引是0
            }
        });

        mAdapter = mPresenter.getAdapter(this);
        recyclerView.setAdapter(mAdapter);//由外部注入
        recyclerView.setLayoutManager(mPresenter.getLayoutManager(this));//由外部注入
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSuccess(Object obj) {
        //数据请求成功则会回调该方法
        swipeRefreshLayout.setRefreshing(false);
        //可以在这里向Adapter中添加数据
        //我们这里的展示仅仅用于模拟
        mAdapter.notifyDataSetChanged();
        //为了演示数据真的被传过来了，我们使用吐司来看一下
        Toast.makeText(this, obj.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(Object obj) {
        swipeRefreshLayout.setRefreshing(false);
        //数据请求失败则会回调该方法
        //可以做一些失败的处理

    }
}
